package com.zzb.weikeautosign.console;

import com.zzb.weikeautosign.Constants;
import com.zzb.weikeautosign.RedisUtil;
import com.zzb.weikeautosign.user.UserManager;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 瑱彬 on 2016/3/3.
 */
public class ConsoleExcutor {
    private static final Logger log = Logger.getLogger(ConsoleExcutor.class);

    final static Pattern helpPattern = Pattern.compile("^(\\?|help)$");
    final static Pattern exitPattern = Pattern.compile("^shutdown$");
    final static Pattern addP = Pattern.compile("^add.*$");
    final static Pattern addUserP = Pattern.compile("-u (.*) (-p.*)$");
    final static Pattern addPassP = Pattern.compile("-p (.*)$");
    final static Pattern addPassPWithType = Pattern.compile("-p (.*) (-t.*)$");
    final static Pattern addTypeP = Pattern.compile("-t (.*)$");
    final static Pattern delP = Pattern.compile("^del.*$");
    final static Pattern delUserP = Pattern.compile("^del (.*)$");
    final static Pattern listP = Pattern.compile("^list.*$");
    final static Pattern listLevelP = Pattern.compile("^list (.*)$");
    public static String execute(String s){
        log.info("--"+s);
        String retString = "";
        if (StringUtils.isNotBlank(s)) {
            //退出
            if (exitPattern.matcher(s).matches()) {
                log.info( "通过指令退出系统");
                System.exit(0);
            }
            //帮助
            if (helpPattern.matcher(s).matches()) {
                retString = ("---** 帮 助 **---\nq|exit|quit 退出客户端\nshutdown  关闭服务器\nadd -u 用户名 -p 密码[ -t 用户类型]; -t 参数必须在0~3范围内!0-免费用户,1-月费,2-学期,3-一年\ndel 用户名\nlist[ level];level 在0~4范围内!0-免费用户,1-月费,2-学期,3-一年,4-过期");
                log.info(retString);
            }
            //删除
            if (delP.matcher(s).matches()){
                Matcher nameMatcher = delUserP.matcher(s);
                String username = null;
                while(nameMatcher.find()){
                    username = nameMatcher.group(1);
                }
                if(StringUtils.isNotBlank(username)){
                    UserManager.del(username);
                    retString = ("删除用户:"+username);
                    log.info(retString);
                }else{
                    retString = ("请输入del 用户名");
                    log.error(retString);
                }
            }

            //新增
            if (addP.matcher(s).matches()) {
                Matcher userMatcher = addUserP.matcher(s);
                String username = null;
                String password = null;
                int type = 0;
                while (userMatcher.find()) {
                    username = userMatcher.group(1);
                }
                Matcher passMatcher = addPassPWithType.matcher(s);
                while (passMatcher.find()) {
                    password = passMatcher.group(1);
                }
                if (StringUtils.isNotBlank(password)) {
                    Matcher typeMatcher = addTypeP.matcher(s);
                    while ((typeMatcher.find())) {
                        try {
                            type = Integer.valueOf(typeMatcher.group(1));
                        } catch (NumberFormatException e) {
                            type = -1;
                            retString = ("-t 参数必须为数字!0-免费用户,1-月费,2-学期,3-一年");
                            log.error(retString);
                        }
                    }
                } else {
                    passMatcher = addPassP.matcher(s);
                    while (passMatcher.find()) {
                        password = passMatcher.group(1);
                    }
                }
                if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
                    if(!(type < 0 || type > 3)){
                        UserManager.add(username, password, type);
                        retString = ("成功将用户加入线程池");
                        log.info(retString);

                    }else if(type != -1){
                        retString =("-t 参数必须在0~3范围内!0-免费用户,1-月费,2-学期,3-一年");
                        log.error(retString);

                    }
                } else {
                    retString = ("请输入add -u 用户名 -p 密码[ -t 用户类型]; -t 参数必须在0~3范围内!0-免费用户,1-月费,2-学期,3-一年");
                    log.info(retString);
                }
            }
            //显示列表
            if(listP.matcher(s).matches()){
                Matcher listLevelMatcher = listLevelP.matcher(s);
                int level = 9; //列出所有用户
                boolean flag = true;
                if(listLevelMatcher.matches()){
                    try {
                        level = Integer.valueOf(listLevelMatcher.group(1));
                        if(level < 0 || level > 4)throw new NumberFormatException();
                    } catch (NumberFormatException e) {
                        level = 9;
                        flag = false;
                        retString =("参数必须在0~4范围内!0-免费用户,1-月费,2-学期,3-一年,4-过期用户");
                        log.error(retString);
                    }
                }
                if(flag){
                    Jedis jedis = RedisUtil.getJedis();
                    Map<String,String> freeUserMap = null;
                    Map<String,String> thirtyDayUserMap = null;
                    Map<String,String> halfYearUserMap = null;
                    Map<String,String> oneYearUserMap = null;
                    Map<String,String> overTimeUserMap = null;
                    if(jedis != null){
                        switch (level){
                            case 0 : freeUserMap= jedis.hgetAll(Constants.FREEUSER);break;
                            case 1 : thirtyDayUserMap=jedis.hgetAll(Constants.THIRYDAYUSER);break;
                            case 2 : halfYearUserMap=jedis.hgetAll(Constants.HALFYEARUSER);break;
                            case 3 : oneYearUserMap=jedis.hgetAll(Constants.ONEYEARUSER);break;
                            case 4 : overTimeUserMap=jedis.hgetAll(Constants.OVERTIMEUSER);break;
                            default: {
                                freeUserMap= jedis.hgetAll(Constants.FREEUSER);
                                thirtyDayUserMap=jedis.hgetAll(Constants.THIRYDAYUSER);
                                halfYearUserMap=jedis.hgetAll(Constants.HALFYEARUSER);
                                oneYearUserMap=jedis.hgetAll(Constants.ONEYEARUSER);
                                overTimeUserMap=jedis.hgetAll(Constants.OVERTIMEUSER);

                            }
                        }
                        StringBuilder sb = new StringBuilder("用户列表:\n");
                        if(MapUtils.isNotEmpty(freeUserMap)){
                            sb.append("-免费试用用户:\n");
                            freeUserMap.forEach((s1, s2) -> sb.append(s2));
                        }
                        if(MapUtils.isNotEmpty(thirtyDayUserMap)){
                            sb.append("-月费用户:\n");
                            thirtyDayUserMap.forEach((s1, s2) -> sb.append(s2));
                        }
                        if(MapUtils.isNotEmpty(halfYearUserMap)){
                            sb.append("-一学期用户:\n");
                            halfYearUserMap.forEach((s1, s2) -> sb.append(s2));
                        }
                        if(MapUtils.isNotEmpty(oneYearUserMap)){
                            sb.append("-一学年用户:\n");
                            oneYearUserMap.forEach((s1, s2) -> sb.append(s2));
                        }
                        if(MapUtils.isNotEmpty(overTimeUserMap)){
                            sb.append("-过期用户:\n");
                            overTimeUserMap.forEach((s1, s2) -> sb.append(s2));
                        }
                        retString = sb.toString();
                    }else{
                        retString = "服务器redis出错";
                        log.error(retString);
                    }
                }
            }
            //非命令提示
            if(StringUtils.isBlank(retString)){
                retString = ("输入help查看帮助");
                log.error(retString);
            }
        }
        return retString;
    }
}
